<template>
  <div class="login-container">
    <el-card class="box-card">
      <!-- 标题 -->
      <div class="title"></div>
      <!-- 使用表单 -->
      <!-- model数据项  rules规则对象-->
      <el-form ref="form" :model="user" :rules="rules">
        <!-- 一个 el-form-item就是一行-->
        <el-form-item prop="uname">
          <el-input
            placeholder="小主请输入注册用户名"
            prefix-icon="el-icon-user"
            v-model="user.uname"
          ></el-input>
        </el-form-item>
        <el-form-item prop="pass" prefix-icon>
          <!-- 输入框前面的字体图标 -->
          <!-- 小眼睛带功能 -->
          <el-input
            v-model="user.pass"
            type="password"
            placeholder="这里输入注册密码小主"
            prefix-icon="el-icon-unlock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="repass">
          <!--  prefix-icon输入框前面的字体图标  -->
          <!-- 小眼睛带功能 -->
          <el-input
            v-model="user.repass"
            type="password"
            placeholder="这里再次输入密码哦小主"
            prefix-icon="el-icon-unlock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="reg">点我注册咯....</el-button>
        </el-form-item>
        <!-- 是否添加下划线 -->
        <el-link :underline="false" @click="$router.push('/login')">有账户了点这里小主</el-link>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { regAPI } from '@/api/user'
export default {
  name: 'MyReg',
  data() {
    // value当前项的值 , callback是否通过
    const validatePass2 = (rule, value, callback) => {
      if (value === this.user.pass) {
        callback()
      } else {
        callback(new Error('两次密码不一致'))
      }
    }
    return {
      user: {
        uname: '',
        pass: '',
        repass: ''
      },
      rules: {
        // 验证用户的规则
        uname: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        // 验证密码的规则
        pass: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        // 验证确认密码的规则
        repass: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reg() {
      this.$refs.form.validate(async valid => {
        // 如果对错了就取反变成true进入if判断进去
        if (!valid) return
        const data = {
          username: this.user.uname,
          password: this.user.pass,
          repassword: this.user.repass
        }
        const { data: res } = await regAPI(data)
        if (res.code === 0) {
          // 注册成功的提示
          this.$message.success(res.message)
          this.$refs.form.resetFields() // 重置表单
          this.$router.push('/login') // 跳转登录页
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  background: url(@/assets/images/login_bg.jpg) no-repeat;
  background-size: cover;
  height: 100%;
  .el-card {
    width: 400px;

    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border: 2px solid #000;
    border-radius: 10px;
    .title {
      height: 40px;
      background: url(@/assets/images/login_title.png) no-repeat top center;
    }
    .el-button {
      width: 100%;
      text-align: center;
      font-weight: 500;
      font-size: 16px;
    }
  }
}
</style>
